﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenTK.Graphics.OpenGL;

namespace Lichtspiel.Display
{
    internal class GLHelper
    {
        public static void Scale(float x, float y, float scaleX, float scaleY) {
            GL.Translate(x, y, 0.0);
            GL.Scale(scaleX, scaleY, 0.0f);
            GL.Translate(-x, -y, 0.0);
        }

        public static void Scale(float scaleX, float scaleY) {
            GL.Scale(scaleX, scaleY, 0.0f);
        }

        public static void Rotate(float x, float y, float degrees) {
            GL.Translate(x, y, 0.0);
            GL.Rotate(degrees, 0.0f, 0.0f, 1.0f);
            GL.Translate(-x, -y, 0.0);
        }

        public static void Rotate(float degrees) {
            GL.Rotate(degrees, 0.0f, 0.0f, 1.0f);
        }

        public static void Translate(float x, float y) {
            GL.Translate(x, y, 0.0f);
        }

        public static void ResetTransforms() {
            GL.LoadIdentity();
            GL.Translate(0.375, 0.375, 0.0); // Move slightly for pixel precise drawing
        }

        public static void EnableBlend(BlendMode blendMode, Color color) {
            GL.Enable(EnableCap.Blend);

            BlendingFactorSrc src = BlendingFactorSrc.SrcAlpha;
            BlendingFactorDest dest = BlendingFactorDest.OneMinusSrcAlpha;
            BlendEquationMode mode = BlendEquationMode.FuncAdd;

            switch (blendMode) {
                case BlendMode.Multiply:
                    src = BlendingFactorSrc.DstColor;
                    GL.Color4(color.R * color.A, color.G * color.A, color.B * color.A, color.A);
                    break;
                case BlendMode.Add:
                    src = BlendingFactorSrc.SrcAlpha;
                    dest = BlendingFactorDest.One;
                    GL.Color4(color.R, color.G, color.B, color.A);
                    break;
                default:
                    // Blendmode alpha (default)
                    GL.Color4(color.R, color.G, color.B, color.A);
                    break;
            }

            GL.BlendEquation(mode);
            GL.BlendFunc(src, dest);
        }

        public static void DisableBlend() {
            GL.Disable(EnableCap.Blend);

            GL.Color4(1f, 1f, 1f, 1f);
        }
    }
}
